
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "WARRIOR" or not module then return end

local hasRevengeImprovedGlyph = false
local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .8
	elseif size < 0.6 then
		size = baseSize * 1.0
	else
		size = baseSize * 1.2
	end
	return size / 2
end

local L_SWORD_AND_BOARD = "Sword and Board"
local L_OVERPOWER = "Overpower"
local L_SUDDEN_DEATH = "Sudden Death"
local L_TASTE_FOR_BLOOD = "Taste for Blood"
local L_EXECUTE = "Execute"
local L_SUNDER_ARMOR = "Sunder Armor"
local L_WEAKENED_ARMOR = "Weakened Armor"
if GetLocale()=="koKR" then
L_SWORD_AND_BOARD = "검과 방패"
L_OVERPOWER = "제압"
L_SUDDEN_DEATH = "급살"
L_TASTE_FOR_BLOOD = "피의 맛"
L_EXECUTE = "마무리 일격"
L_SUNDER_ARMOR = "방어구 가르기"
L_WEAKENED_ARMOR = "방어구 약화"
end

module.config = {
	[L_SWORD_AND_BOARD] = {
		spellid = 46953
	},
	[L_TASTE_FOR_BLOOD] = {
		spellid = 56638
	},
	[L_SUDDEN_DEATH] = {
		spellid = 29725
	},
}

function module:init()
	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")
	self.addon.mainFrame:RegisterEvent("GLYPH_ADDED")
	self.addon.mainFrame:RegisterEvent("GLYPH_REMOVED")
	--self.addon.mainFrame:RegisterEvent("VARIABLES_LOADED")
	self:update()
end

function module:update()
	self.primary = GetSpecialization()
	for auraName, t in pairs(self.config) do
		if t.talent and t.talent > 0 then
			local name, iconTexture, tier, column, selected, available = GetTalentInfo(t.talent);
			if name and available then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		else
			self.config[auraName].use = false
			if IsPlayerSpell(t.spellid) then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		end
	end
end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
module.GLYPH_ADDED = module.LEARNED_SPELL_IN_TAB
module.GLYPH_REMOVED = module.LEARNED_SPELL_IN_TAB
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end

function module:getTargetText()
	local size = self.addon.db.fontSizeHealth
	local icon, text = "", ""
	local aura, rank, icon, count, debufType, duration, expirationTime

	if self.config[L_TASTE_FOR_BLOOD].use then
		aura = UnitAura("player", L_TASTE_FOR_BLOOD, nil, "HELPFUL")
		local usable, nomana = IsUsableSpell(L_OVERPOWER)
		_, _, icon = GetSpellInfo(L_OVERPOWER)
		if aura or usable then
			text = (":|T%s:%d|t"):format(icon, size*1.2 / 2)
		end
	end

	if self.config[L_SUDDEN_DEATH].use then
		aura, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_SUDDEN_DEATH, "HELPFUL")
		if aura then
			_, _, icon = GetSpellInfo(86346)
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

	if self.config[L_SWORD_AND_BOARD].use then
		aura, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_SWORD_AND_BOARD, nil, "HELPFUL")
		if aura then
			_, _, icon = GetSpellInfo(23922) -- Shield Slam
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

	return self:getAuraCount("target", L_WEAKENED_ARMOR, "HARMFUL") .. text
end
